package com.controller;

import com.dao.CommentDao;
import com.po.Comment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet({"/openNote/comment", "/createComment", "/function/selectMyComment"})
public class commentSystem extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("id") != null) {
            if ("/openNote/comment".equals(servletPath)) {
                doSelectComment(request, response);
            } else if ("/createComment".equals(servletPath)) {
                doCreateComment(request, response);
            } else if ("/function/selectMyComment".equals(servletPath)) {
                doMycomment(request, response);
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }

    /**
     * 查看我自己的评论区
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doMycomment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int note_id = Integer.parseInt(request.getParameter("note_id"));

        List<Comment> comments = CommentDao.getComment(note_id);

        String note_name = request.getParameter("note_name");
        String note_content = request.getParameter("note_content");
        request.setAttribute("note_name", note_name);
        request.setAttribute("note_content", note_content);

        request.setAttribute("comments", comments);
        request.getRequestDispatcher("/comment/myComment.jsp").forward(request, response);
    }

    /**
     * 创建评论
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doCreateComment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");

        String comment_content = request.getParameter("comment_content");
        int note_id = Integer.parseInt(request.getParameter("note_id"));

        String note_name = request.getParameter("note_name");
        String note_content = request.getParameter("note_content");

        if (comment_content.length() >= 1) {
            int count = CommentDao.createComment(id, note_id, comment_content);
            if (count == 1) {
                out.print("<title>创建成功</title>");
                out.println("创建成功");
                out.println("<a href=\"" + request.getContextPath() + "/openNote/comment?note_id=" + note_id + "&note_name=" + note_name + "&note_content=" + note_content + "\">返回评论区</a>");
            } else {
                out.println("失败<br>");
                out.println("<a href=\"" + request.getContextPath() + "/function/openEnvelope\">总信封页面</a>");
            }
        }else{
            out.print("评论不能为空");
        }
    }

    /**
     * 查找某个评论
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doSelectComment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession(false);
        int note_id = Integer.parseInt(request.getParameter("note_id"));
        String note_name = request.getParameter("note_name");
        String note_content = request.getParameter("note_content");
        request.setAttribute("note_id", note_id);
        request.setAttribute("note_name", note_name);
        request.setAttribute("note_content", note_content);

        session.setAttribute("note_name",note_name);
        session.setAttribute("note_content",note_content);

        List<Comment> comment = CommentDao.getComment(note_id);
        request.setAttribute("comment", comment);
        request.getRequestDispatcher("/comment/openComment.jsp").forward(request, response);
    }
}
